<?php
class Default_Model_Tour extends Zend_Db_Table{

	protected $_name = TOUR_TABLE;
	protected $_primary = 'tour_id';

	private $TOUR_ID = 'tour_id';
	private $TOUR_NAME = 'name';
	private $TOUR_CREATED_TIME = 'created_time';

	protected $db;

	public function __construct(){
		//Goi ket noi voi Zend Db
		$this->db = Zend_Registry::get('connectDB');
	}

	protected $_messageError = null;

	public function countTour(){
		$select = $this->db->select()
		->from(TOUR_TABLE,array('COUNT(tour_id) AS totalTour'));
		$result = $this->db->fetchOne($select);
		return $result;
	}

	public function listAllTour(){
		$select = $this->db->select()->from(TOUR_TABLE);
		$select->order($this->TOUR_ID.' DESC');
		//$result  = $this->db->fetchAll($select);
		return $select;
	}

	public function getTourInfoById($tourId) {
		$select = $this->db->select()->from(TOUR_TABLE)->where($this->TOUR_ID.' = ?',$tourId);
		$result  = $this->db->fetchAll($select);
		return $result;
	}
	
	// Delete tour
	public function deleteTourById($tourId){
		try {
			$this->db->delete(TOUR_TABLE,$this->TOUR_ID.' = ' . $tourId);
			return true;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
			return false;
		}
	}

	// get tour statistic
	public function tourStatisticByMonth($month) {
		try {
			$select = $this->db->select()->from(TOUR_TABLE)->where($this->TOUR_CREATED_TIME.' LIKE "%'.$month.'%"');
			$result  = $this->db->fetchAll($select);
			return $result;
		} catch (Exception $e) {
			$this->_messageError = $e->getMessage();
		}
	}
	
	public function getError(){
		return $this->_messageError;
	}
}